package raceReservationServer.raceServerGateway;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.axis2.AxisFault;

import corbaServer.corba.RaceDTO;

import raceReservationServer.raceServerGateway.proxies.ArabaRacesWSStub;

public class WSRaceServerGateway implements IRaceServerGateway {

private ArabaRacesWSStub stub = null;
	
	private String serviceUri;
	private String serviceName;
	public WSRaceServerGateway(String serviceUri) 
	{
		StringTokenizer st = new StringTokenizer(serviceUri);
		this.serviceUri = st.nextToken();
		this.serviceName = st.nextToken();
	}

	public List<RaceDO> getRaces() 
	{
		ArabaRacesWSStub stub = null;
		try {
			stub = new ArabaRacesWSStub(serviceUri);
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<RaceDO> result = new ArrayList<RaceDO>();
		raceReservationServer.raceServerGateway.proxies.ArabaRacesWSStub.RaceDTO[] racesList = null;
		try 
		{
			racesList = stub.getRaces();
		} 
		catch (RemoteException e1) 
		{
			e1.printStackTrace();
		}
		for (int i = 0; i < racesList.length; i++)
		{
			RaceDO race = new RaceDO(racesList[i].getName(), racesList[i].getDistance(), racesList[i].getDate(), racesList[i].getPlaces());
			System.out.println(race.getName());
			result.add(race);
		}
		return result;
	}

	@Override
	public String getServer() 
	{
		return "ArabaRacesWS";
	}
}
